package com.zablert.mainactivity.recyclerview

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.zablert.mainactivity.BaseRouteConstants
import com.zablert.mainactivity.R
import com.zablert.mainactivity.databinding.ActivityItemAnimatorBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import zyc.zalbert.activity.CoreBVMActivity
import zyc.zalbert.extensions.logd
import zyc.zalbert.utils.TestUtils
import zyc.zalbert.view.recycleview.animators.FadeInAnimator
import zyc.zalbert.view.recycleview.animators.FadeInLeftAnimator

@Route(path = BaseRouteConstants.ITEM_ANIMATOR_PATH)
class ItemAnimatorActivity : CoreBVMActivity<ActivityItemAnimatorBinding, ItemAnimatorViewModel>() {
    private var mAdapter: ItemAnimatorAdapter? = null
    private val itemAnimator = FadeInLeftAnimator()
    override fun layoutId(): Int {
        return R.layout.activity_item_animator
    }

    override fun initViewModel(): ItemAnimatorViewModel {
        return ItemAnimatorViewModel()
    }

    override fun onInitContent() {
        super.onInitContent()
        binding.rvDemo.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        mAdapter = ItemAnimatorAdapter()
        binding.rvDemo.adapter = mAdapter
        binding.rvDemo.itemAnimator = itemAnimator
//    adapter.notifyItemMoved(int)
//
//    adapter.notifyItemChanged(int)
//    adapter.notifyItemRangeChanged(int, int)
//
//    adapter.notifyItemInserted(int)
//    adapter.notifyItemRangeInserted(int, int)
//
//    adapter.notifyItemRemoved(int)
//    adapter.notifyItemRangeRemoved(int, int)
        mAdapter?.setItems(viewModel.items)
    }

    override fun onInitViewAction() {
        super.onInitViewAction()
        binding.btnAdd.setOnClickListener {

            viewModel.items.add(ItemAnimatorParam("", ""))
            mAdapter?.setItems(viewModel.items)
            mAdapter?.notifyItemInserted(1)
        }
        binding.btnRemove.setOnClickListener {
            viewModel.items.removeAt(1)
            mAdapter?.setItems(viewModel.items)
            mAdapter?.notifyItemRemoved(1)

        }
        binding.btnUpdate.setOnClickListener {
            viewModel.items = TestUtils.forData(ItemAnimatorParam("", ""), 10)
            mAdapter?.setItems(viewModel.items)
            mAdapter?.notifyDataSetChanged()
        }
    }
}
